package com.sanshi.project.assets.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.admin.api.dto.UnitDTO;
import com.sanshi.project.admin.api.entity.CodeRuleEntity;
import com.sanshi.project.admin.api.entity.UnitEntity;
import com.sanshi.project.assets.service.CodeRuleService;
import com.sanshi.project.assets.service.UnitService;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import com.sanshi.project.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 单元楼
 *
 * @author nikon
 * @date 2024-02-08 20:01:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/unit" )
@Tag(description = "unit" , name = "单元楼管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class UnitController {

    private final  UnitService unitService;
	private final CodeRuleService codeRuleService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param unit 单元楼
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('assets_unit_view')" )
    public R getUnitPage(@ParameterObject Page page, @ParameterObject UnitDTO unit) {
        LambdaQueryWrapper<UnitEntity> wrapper = Wrappers.lambdaQuery();
        // 编码
		wrapper.like(StrUtil.isNotBlank(unit.getCode()),UnitEntity::getCode,unit.getCode());
        // 单元名称
		wrapper.like(StrUtil.isNotBlank(unit.getName()),UnitEntity::getName,unit.getName());
        // 项目
		wrapper.in(ArrayUtil.isNotEmpty(unit.getProjectId()),UnitEntity::getProjectId,unit.getProjectId());
        // 楼栋
		wrapper.in(ArrayUtil.isNotEmpty(unit.getBuildingId()),UnitEntity::getBuildingId,unit.getBuildingId());
        // 单元面积
		wrapper.in(ArrayUtil.isNotEmpty(unit.getFloorArea()),UnitEntity::getFloorArea,unit.getFloorArea());
        // 是否有电梯
		wrapper.in(ArrayUtil.isNotEmpty(unit.getIsElevator()),UnitEntity::getIsElevator,unit.getIsElevator());
        wrapper.orderByDesc(UnitEntity::getUpdateTime);
        return R.ok(unitService.page(page, wrapper));
    }


    /**
     * 通过id查询单元楼
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('assets_unit_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(unitService.getById(id));
    }

    /**
     * 新增单元楼
     * @param unit 单元楼
     * @return R
     */
    @Operation(summary = "新增单元楼" , description = "新增单元楼" )
    @SysLog("新增单元楼" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('assets_unit_add')" )
    public R save(@RequestBody UnitEntity unit) {
		CodeRuleEntity codeRuleEntity = new CodeRuleEntity();
		codeRuleEntity.setType("25");
		R<String> resultCode = codeRuleService.generateCode(codeRuleEntity);
		if (resultCode.isOk()) {
			// 赋予单元编码
			unit.setCode(resultCode.getData());
		} else {
			return resultCode;
		}
        return R.ok(unitService.save(unit));
    }

    /**
     * 修改单元楼
     * @param unit 单元楼
     * @return R
     */
    @Operation(summary = "修改单元楼" , description = "修改单元楼" )
    @SysLog("修改单元楼" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('assets_unit_edit')" )
    public R updateById(@RequestBody UnitEntity unit) {
        return R.ok(unitService.updateById(unit));
    }

    /**
     * 通过id删除单元楼
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除单元楼" , description = "通过id删除单元楼" )
    @SysLog("通过id删除单元楼" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('assets_unit_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(unitService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param unit 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('assets_unit_export')" )
    public List<UnitEntity> export(UnitEntity unit,Long[] ids) {
        return unitService.list(Wrappers.lambdaQuery(unit).in(ArrayUtil.isNotEmpty(ids), UnitEntity::getId, ids));
    }

	/**
	 * 单元全量查询
	 * @param unit 单元楼管理
	 * @return
	 */
	@Operation(summary = "单元全量查询", description = "单元全量查询")
	@GetMapping("/list" )
	@PreAuthorize("@pms.hasPermission('assets_unit_view')" )
	public R getUnitList(@ParameterObject UnitDTO unit) {
		LambdaQueryWrapper<UnitEntity> wrapper = Wrappers.lambdaQuery();
		// 编码
		wrapper.like(StrUtil.isNotBlank(unit.getCode()),UnitEntity::getCode,unit.getCode());
		// 单元名称
		wrapper.like(StrUtil.isNotBlank(unit.getName()),UnitEntity::getName,unit.getName());
		// 项目
		wrapper.in(ArrayUtil.isNotEmpty(unit.getProjectId()),UnitEntity::getProjectId,unit.getProjectId());
		// 楼栋
		wrapper.in(ArrayUtil.isNotEmpty(unit.getBuildingId()),UnitEntity::getBuildingId,unit.getBuildingId());
		// 单元面积
		wrapper.in(ArrayUtil.isNotEmpty(unit.getFloorArea()),UnitEntity::getFloorArea,unit.getFloorArea());
		// 是否有电梯
		wrapper.in(ArrayUtil.isNotEmpty(unit.getIsElevator()),UnitEntity::getIsElevator,unit.getIsElevator());
		wrapper.orderByDesc(UnitEntity::getUpdateTime);
		return R.ok(unitService.list(wrapper));
	}
}